// components/my/my.js
const app = getApp();
import Dialog from '@vant/weapp/dialog/dialog';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
  },

  /**
   * 组件的初始数据
   */
  data: {
    userInfo: {}, //avatarUrl
    realInfo: {},
    authorization: null,
    listData: [],
    dialogVisible: false
  },

  ready() {
    this.init();
  },

  /**
   * 组件的方法列表
   */
  methods: {
    init() {
      this.setData({
        authorization: {
          accountId: app.globalData.accountId,
          enableAgent: app.globalData.enableAgent
        }
      })
      app.$request.personalHome()
      .then(res=>{
        if(res.code === 1 && res.data) {
          this.setData({
            userInfo: res.data
          });
        }
      })
      .then(_=>{
        return app.$request.personalQueryrealinfo()
      })
      .then(res => {
        if (res.code === 1 && res.data) {
          this.setData({
            realInfo: res.data
          });
        }else{
          Dialog.alert({
            context: this,
            title: '提示',
            message: res.message
          })
        }
      })
    },
    myPhone() {
      wx.navigateTo({
        url: '../phoneNumber/phoneNumber',
        events: {
          acceptDataFromOpenedPage: data => {
            console.log(data)
            this.init();
          }
        },
        success: res => {
          res.eventChannel.emit('acceptDataFromOpenerPage', { data: this.data.userInfo.phone })
        }
      })
    },
    realNameAuthentication() {
      wx.navigateTo({
        url: '../realNameAuthentication/realNameAuthentication',
        events: {
          acceptDataFromOpenedPage: data => {
            console.log(data)
            this.init();
          }
        },
        success: res => {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('acceptDataFromOpenerPage', { data: {
            phone: this.data.userInfo.phone,
            realName: this.data.userInfo.realName,
            realCard: this.data.userInfo.realCard,
          } })
        }
      })
    },
    cash() {
      wx.navigateTo({
        url: '../cash/cash',
        events: {
          // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
          acceptDataFromOpenedPage: function (data) {
            console.log(data)
          },
          someEvent: function (data) {
            console.log(data)
          }
        },
        success: res => {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('acceptDataFromOpenerPage', { data: this.data.userInfo.balance })
        }
      })
    },
    alipayBind() {
      wx.navigateTo({
        url: '../alipayBind/alipayBind',
        events: {
          // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
          acceptDataFromOpenedPage: data =>  {
            console.log(data)
            this.init();
          },
          someEvent: function (data) {
            console.log(data)
          }
        },
        success: res => {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('acceptDataFromOpenerPage', { data: {
            alipayName: this.data.userInfo.alipayName,
            alipayAccount: this.data.userInfo.alipayAccount
          } })
        }
      })
    },
    handleTab1Click() {
      if(!this.data.realInfo.realName) {
        Dialog.alert({
          context: this,
          title: '提示',
          message: '请先进行实名认证'
        })
        return;
      }
      if (this.data.userInfo.isAgent) {
        Dialog.alert({
          context: this,
          title: '提示',
          message: '您当前已是经纪人'
        })
        return;
      }
      Dialog.alert({
        context: this,
        title: '申请经纪人',
        message: `姓名：${this.data.realInfo.realName}`
      }).then(_=>{
        app.$request.applyAgent()
      })
    },
    handleTab2Click() {
      // Dialog.alert({
      //   context: this,
      //   title: '提示',
      //   message: '功能正在开发中，敬请期待！'
      // })
      this.setData({
        dialogVisible: true
      })
    },
    handleTab3Click() {
      Dialog.alert({
        context: this,
        title: '提示',
        message: '功能正在开发中，敬请期待！'
      })
    },
    goDetail(event) {
      let item = event.currentTarget.dataset.item;
      wx.navigateTo({
        url: '../recruitInfo/recruitInfo',
        events: {
          acceptDataFromOpenedPage: data => {
            console.log(data)
          }
        },
        success: res => {
          res.eventChannel.emit('acceptDataFromOpenerPage', { eid: item.eid })
        }
      })
    },
  }
})
